﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OpalisConsole.EndUserService;
using System.Windows.Documents;

namespace OpalisConsole.Views.Administration
{
    public partial class LanguageControl : Page
    {
        private Dictionary<string, string> modifiedControlText = new Dictionary<string, string>();
        public LanguageControl()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!(bool)App.Current.Resources["FreshStartAdmin"])
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
            else
            {
                busyIndicatorMain.IsBusy = true;
                busyIndicatorMain.BusyContent = "Loading Control Data";
                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.GetControlIdentifiersCompleted += new EventHandler<GetControlIdentifiersCompletedEventArgs>(proxy_GetControlIdentifiersCompleted);
                proxy.GetControlIdentifiersAsync();
            }
        }

        void proxy_GetControlIdentifiersCompleted(object sender, GetControlIdentifiersCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;

            foreach (string controlName in e.Result)
            {
                BusyIndicator controlIdentiferTextBusy = new BusyIndicator();
                controlIdentiferTextBusy.Name = "busyIndicator" + controlName;
                controlIdentiferTextBusy.IsBusy = true;


                StackPanel contentPanel = new StackPanel();
                contentPanel.Name = "contentPanel" + controlName;
                contentPanel.Orientation = Orientation.Vertical;

                StackPanel controlPanel = new StackPanel();
                controlPanel.Name = "controlPanel" + controlName;
                controlPanel.Orientation = Orientation.Horizontal;

                Label controlIdentifierLabel = new Label();
                controlIdentifierLabel.Name = "label" + controlName;
                controlIdentifierLabel.Margin = new Thickness(5);
                controlIdentifierLabel.Content = controlName;
                controlIdentifierLabel.Height = 125;
                controlIdentifierLabel.Width = 160;
                controlPanel.Children.Add(controlIdentifierLabel);


                RichTextBox controlIdentifierText = new RichTextBox();
                controlIdentifierText.Height = 125;
                controlIdentifierText.Width = 450;
                controlIdentifierText.Margin = new Thickness(5);
                controlIdentifierText.Name = "txt" + controlName;
                controlPanel.Children.Add(controlIdentifierText);

                StackPanel btnPanel = new StackPanel();
                btnPanel.Name = "btnPanel" + controlName;
                btnPanel.Orientation = Orientation.Horizontal;
                btnPanel.HorizontalAlignment = HorizontalAlignment.Left;
                btnPanel.Margin = new Thickness(410, 0, 0, 0);
               
                Button btnUpdateControl = new Button();
                btnUpdateControl.Name = "btnUpdate" + controlName;
                btnUpdateControl.Content = "Update";
                btnUpdateControl.Margin = new Thickness(5);
                btnUpdateControl.Width = 100;
                btnUpdateControl.Click += new RoutedEventHandler(btnUpdateControl_Click);

                Button btnResetControl = new Button();
                btnResetControl.Name = "btnReset" + controlName;
                btnResetControl.Content = "Reset";
                btnResetControl.Margin = new Thickness(5);
                btnResetControl.Width = 100;
                btnResetControl.Click += new RoutedEventHandler(btnResetControl_Click);

                btnPanel.Children.Add(btnUpdateControl);
                btnPanel.Children.Add(btnResetControl);

                contentPanel.Children.Add(controlPanel);
                contentPanel.Children.Add(btnPanel);
                controlIdentiferTextBusy.Content = contentPanel;

                LanguageControlContent.Children.Add(controlIdentiferTextBusy);

                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.GetControlIdentifierTextCompleted += new EventHandler<GetControlIdentifierTextCompletedEventArgs>(proxy_GetControlIdentifierTextCompleted);
                proxy.GetControlIdentifierTextAsync(controlName, boxGroupName.Text);
                 
            }
        }
        
        void proxy_GetControlIdentifierTextCompleted(object sender, GetControlIdentifierTextCompletedEventArgs e)
        {
            ArrayOfString returnData = e.Result;
            string controlName = returnData[0];
            string controlText = returnData[1];

            RichTextBox controlIdentifierTextBox = (RichTextBox)LayoutRoot.FindName("txt" + controlName);
            BusyIndicator busyIndicator = (BusyIndicator)LayoutRoot.FindName("busyIndicator" + controlName);
            busyIndicator.IsBusy = false;
            
            Run text = new Run();
            text.Text = controlText;
            Paragraph paragraph = new Paragraph();
            paragraph.Inlines.Add(text);

            for (int i = controlIdentifierTextBox.Blocks.Count - 1; i >= 0; i--)
            {
                controlIdentifierTextBox.Blocks.RemoveAt(i);
            }

            controlIdentifierTextBox.Blocks.Add(paragraph);
            
        }

        void btnResetControl_Click(object sender, RoutedEventArgs e)
        {
            Button btnReset = (Button)sender;

            string controlName = btnReset.Name.ToString().Substring(8);

            BusyIndicator busyIndicator = (BusyIndicator)LayoutRoot.FindName("busyIndicator" + controlName);
            busyIndicator.IsBusy = true;
         
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();

            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
            proxy.GetControlIdentifierTextCompleted += new EventHandler<GetControlIdentifierTextCompletedEventArgs>(proxy_GetControlIdentifierTextCompleted);
            proxy.GetControlIdentifierTextAsync(controlName, boxGroupName.Text);
        }

        void btnUpdateControl_Click(object sender, RoutedEventArgs e)
        {
            Button btnUpdate = (Button)sender;

            string controlName = btnUpdate.Name.ToString().Substring(9);

            BusyIndicator busyIndicator = (BusyIndicator)LayoutRoot.FindName("busyIndicator" + controlName);
            busyIndicator.IsBusy = true;
            
            RichTextBox controlIdentifierTextBox = (RichTextBox)LayoutRoot.FindName("txt" + controlName);
            controlIdentifierTextBox.SelectAll();
            string controlText = controlIdentifierTextBox.Selection.Text;
            
            EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();

            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
            proxy.UpdateControlIdentifierTextCompleted += new EventHandler<UpdateControlIdentifierTextCompletedEventArgs>(proxy_UpdateControlIdentifierTextCompleted);
            proxy.UpdateControlIdentifierTextAsync(controlName, boxGroupName.Text, controlText);
        }
        
        void proxy_UpdateControlIdentifierTextCompleted(object sender, UpdateControlIdentifierTextCompletedEventArgs e)
        {
            string controlName = e.Result;

            BusyIndicator busyIndicator = (BusyIndicator)LayoutRoot.FindName("busyIndicator" + controlName);
            busyIndicator.IsBusy = false;
        }
        
        private void boxGroupName_GotFocus(object sender, RoutedEventArgs e)
        {

        }
    }
}
